﻿//Copyright (c) 2010 Michael Ey

//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 


package com.gametrak {
	
	import flash.errors.*;
	import flash.events.*;
	import flash.net.Socket;
	
	public class GameTrakService {

		public static var GAME_TRAK_PORT = 8450;

		private static var _instance:GameTrakService;

		public static function GetInstance():GameTrakService{
			if(!_instance)
			{
				_instance = new GameTrakService();
			}
			
			return _instance;
		}
		
		private var _socket:Socket;
		
		private var _leftCordX:Number = 0.0;
		public function get LeftCordX():Number{
			return _leftCordX;
		}
		
		private var _leftCordY:Number = 0.0;
		public function get LeftCordY():Number{
			return _leftCordY;
		}
		
		private var _leftCordZ:Number = 0.0;
		public function get LeftCordZ():Number{
			return _leftCordZ;
		}
		
		private var _rightCordX:Number = 0.0;
		public function get RightCordX():Number{
			return _rightCordX;
		}
		
		private var _rightCordY:Number = 0.0;
		public function get RightCordY():Number{
			return _rightCordY;
		}
		
		private var _rightCordZ:Number = 0.0;
		public function get RightCordZ():Number{
			return _rightCordZ;
		}
		
		private var _bigButton:Boolean = false;
		public function get BigButton():Boolean{
			return _bigButton;
		}

		public function GameTrakService() {
			// constructor code
		}
		
		public function connect():void{
			
			_socket = new Socket();
			_socket.addEventListener(Event.CLOSE, closeHandler);
			_socket.addEventListener(Event.CONNECT, connectHandler);
			_socket.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			_socket.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);
			_socket.connect("localhost", GAME_TRAK_PORT);
		}
		
		public function closeHandler(e:Event):void{
			trace("GameTrak Socket Closed: " + e);
		}
		
		public function connectHandler(e:Event):void{
			trace("Connected to GameTrak Flash Server!");
		}
		
		public function errorHandler(e:IOErrorEvent):void{
			trace("GameTrak error occurred: " + e);
		}
		
		public function secHandler(e:SecurityErrorEvent):void{
			trace("GameTrak error occurred: " + e);
		}
		
		public function dataHandler(e:ProgressEvent):void{
			if(_socket.bytesAvailable >= 14)
			{
				_leftCordX = _socket.readShort() / 32767;
				_leftCordY = _socket.readShort() / 32767;
				_leftCordZ = _socket.readShort() / 32767;
				
				_rightCordX = _socket.readShort() / 32767;
				_rightCordY = _socket.readShort() / 32767;
				_rightCordZ = _socket.readShort() / 32767;
				
				_bigButton = _socket.readShort() == 1;
			}
		}

	}
	
}
